package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.controller.vo;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class BaseVO implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplVOFilePath("BaseVO");
    }



    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();
        code.bl()
                .apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".controller." + localBinding.getSceneEnum().getBasePackage() + "." + table.getBusinessName() + ".vo;")
                .bl()
                .apAndBl("import lombok.*;")
                .apAndBl("import java.math.BigDecimal;")
                .apAndBl("import java.util.*;")
                .apAndBl("import io.swagger.annotations.*;")
                .apAndBl("import javax.validation.constraints.*;")
        ;
        // 处理 Date 字段的引入
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (column.getCreateOperation() && column.getUpdateOperation() && column.getListOperationResult()
                    && "date".equals(column.getJavaType())) {
                code.apAndBl("import org.springframework.format.annotation.DateTimeFormat;");
                code.bl();
                code.apAndBl("import static " + globalBinding.getDateUtilsClassName() + ".FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;");
                break;
            }
        }
        code.bl();
        code
                .apAndBl("/**")
                .apAndBl("* " + table.getClassComment() + " Base VO，提供给添加、修改、详细的子 VO 使用")
                .apAndBl("* 如果子 VO 存在差异的字段，请不要添加到这里，影响 Swagger 文档生成")
                .apAndBl("*/")
                .apAndBl("@Data")
                .apAndBl("public class  " + localBinding.getSceneEnum().getPrefixClass() + "" + table.getClassName() + "BaseVO {")
        ;
        code.bl();
        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName())) continue;
            // 通用操作
            if (column.getCreateOperation() && column.getUpdateOperation() && column.getListOperationResult())
                code.apAndBl(_Column.render(column));
        }
        code.apAndBl("}");
        code.bl();
        return code.toString();
    }

}